#Ejercicio 1
import os
os.environ['USE_PYGEOS'] = '0'
import geopandas as gpd
from fiona import listlayers
#link con los mapas
canadaMapsLink="https://github.com/AnaLuciaChamorro/geoCanadadf/raw/main/maps/countryMaps.gpkg"
#layers in maps
listlayers(canadaMapsLink)
['provinces', 'cities', 'rivers', 'lakes', 'road']
#layer con el mapa de los aeropuertos
canadaAirportsLink="https://github.com/AnaLuciaChamorro/gdfCanada/raw/main/maps/airports_3347.gpkg"
listlayers(canadaAirportsLink)
['airports']
#leemos la data de los layers
airports=gpd.read_file(canadaAirportsLink,layer='airports')
rivers=gpd.read_file(canadaMapsLink,layer='rivers')
provinces=gpd.read_file(canadaMapsLink,layer='provinces')
#leemos la información con los puertos marítimos
import pandas as pd
infoseaports=pd.read_csv(os.path.join("data","UpdatedPub150.csv"))
#vemos las columnas que tenemos
infoseaports.columns.to_list()
['World Port Index Number', 'Region Name', 'Main Port Name', 'Alternate Port Name', 'UN/LOCODE', 'Country Code', 'World Water Body', 'IHO S-130 Sea Area', 'Sailing Direction or Publication', 'Publication Link', 'Standard Nautical Chart', 'IHO S-57 Electronic Navigational Chart', 'IHO S-101 Electronic Navigational Chart', 'Digital Nautical Chart', 'Tidal Range (m)', 'Entrance Width (m)', 'Channel Depth (m)', 'Anchorage Depth (m)', 'Cargo Pier Depth (m)', 'Oil Terminal Depth (m)', 'Liquified Natural Gas Terminal Depth (m)', 'Maximum Vessel Length (m)', 'Maximum Vessel Beam (m)', 'Maximum Vessel Draft (m)', 'Offshore Maximum Vessel Length (m)', 'Offshore Maximum Vessel Beam (m)', 'Offshore Maximum Vessel Draft (m)', 'Harbor Size', 'Harbor Type', 'Harbor Use', 'Shelter Afforded', 'Entrance Restriction - Tide', 'Entrance Restriction - Heavy Swell', 'Entrance Restriction - Ice', 'Entrance Restriction - Other', 'Overhead Limits', 'Underkeel Clearance Management System', 'Good Holding Ground', 'Turning Area', 'Port Security', 'Estimated Time of Arrival Message', 'Quarantine - Pratique', 'Quarantine - Sanitation', 'Quarantine - Other', 'Traffic Separation Scheme', 'Vessel Traffic Service', 'First Port of Entry', 'US Representative', 'Pilotage - Compulsory', 'Pilotage - Available', 'Pilotage - Local Assistance', 'Pilotage - Advisable', 'Tugs - Salvage', 'Tugs - Assistance', 'Communications - Telephone', 'Communications - Telefax', 'Communications - Radio', 'Communications - Radiotelephone', 'Communications - Airport', 'Communications - Rail', 'Search and Rescue', 'NAVAREA', 'Facilities - Wharves', 'Facilities - Anchorage', 'Facilities - Dangerous Cargo Anchorage', 'Facilities - Med Mooring', 'Facilities - Beach Mooring', 'Facilities - Ice Mooring', 'Facilities - Ro-Ro', 'Facilities - Solid Bulk', 'Facilities - Liquid Bulk', 'Facilities - Container', 'Facilities - Breakbulk', 'Facilities - Oil Terminal', 'Facilities - LNG Terminal', 'Facilities - Other', 'Medical Facilities', 'Garbage Disposal', 'Chemical Holding Tank Disposal', 'Degaussing', 'Dirty Ballast Disposal', 'Cranes - Fixed', 'Cranes - Mobile', 'Cranes - Floating', 'Cranes - Container', 'Lifts - 100+ Tons', 'Lifts - 50-100 Tons', 'Lifts - 25-49 Tons', 'Lifts - 0-24 Tons', 'Services - Longshoremen', 'Services - Electricity', 'Services - Steam', 'Services - Navigation Equipment', 'Services - Electrical Repair', 'Services - Ice Breaking', 'Services - Diving', 'Supplies - Provisions', 'Supplies - Potable Water', 'Supplies - Fuel Oil', 'Supplies - Diesel Oil', 'Supplies - Aviation Fuel', 'Supplies - Deck', 'Supplies - Engine', 'Repairs', 'Dry Dock', 'Railway', 'Latitude', 'Longitude']
#renombramos el nombre de los puertos marítimos
infoseaports.rename(columns={'Main Port Name':'PortName'},inplace=True)
#seleccionamos solo las columnas con las que nos quedaremos
infoseaports=infoseaports.loc[:,['PortName', 'Country Code','Latitude', 'Longitude']]
infoseaports.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 3774 entries, 0 to 3773 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 PortName 3774 non-null object 1 Country Code 3774 non-null object 2 Latitude 3774 non-null float64 3 Longitude 3774 non-null float64 dtypes: float64(2), object(2) memory usage: 118.1+ KB
#verificamos que las capas tengan el mismo crs
provinces.crs.to_epsg()==airports.crs.to_epsg()==rivers.crs.to_epsg()
False
#identificamos que crs es el discordante
provinces.crs.to_epsg(),airports.crs.to_epsg(),rivers.crs.to_epsg()
(4326, 3347, 4326)
#cambiamos el crs de las provincias y ríos
rivers=rivers.to_crs(3347)
provinces=provinces.to_crs(3347)
#verificamos que ahora sean correctos
provinces.crs.to_epsg()==airports.crs.to_epsg()==rivers.crs.to_epsg()
True
#creamos un geodataframe de los puertos marítimos
seaports=gpd.GeoDataFrame(data=infoseaports.copy(),
geometry=gpd.points_from_xy(infoseaports.Longitude,
infoseaports.Latitude),
crs=4326)#no está proyectado
seaports_canada=seaports[seaports['Country Code']=='Canada'].copy()
seaports_canada.reset_index(drop=True, inplace=True)
#proyectamos con el crs de Canadá
seaports_canada_3347=seaports_canada.to_crs(3347)
# seleccionamos el subconjunto de los aeropuertos medianos
mediumAirports=airports[airports.kind=='medium_airport']
mediumAirports.reset_index(drop=True, inplace=True)
#ploteamos
base=mediumAirports.plot(color='violet',marker="^")
seaports_canada_3347.plot(color='darkblue',ax=base,alpha=0.5,markersize=3)
<Axes: >
#obtenemos una vista previa de los puertos maritimos
seaports_canada_3347.head()
| PortName | Country Code | Latitude | Longitude | geometry | |
|---|---|---|---|---|---|
| 0 | Port Severn | Canada | 44.800000 | -79.716667 | POINT (7173072.050 1051169.583) |
| 1 | Little Bras D Or | Canada | 46.250000 | -60.300000 | POINT (8562542.390 1720093.723) |
| 2 | Hilton | Canada | 46.250000 | -83.883333 | POINT (6821112.908 1160542.204) |
| 3 | Kagawong | Canada | 45.916667 | -82.250000 | POINT (6952950.534 1141032.855) |
| 4 | Douglastown | Canada | 48.766667 | -64.383333 | POINT (8159882.288 1833159.695) |
#obtenemos una vista previa de los aeropuertos medianos
mediumAirports.head()
| name | kind | latitude_deg | longitude_deg | elevation_ft | region_name | municipality | geometry | |
|---|---|---|---|---|---|---|---|---|
| 0 | Billy Bishop Toronto City Centre Airport | medium_airport | 43.627499 | -79.396202 | 252.0 | Ontario | Toronto | POINT (7224333.646 925621.026) |
| 1 | London Airport | medium_airport | 43.035599 | -81.153900 | 912.0 | Ontario | London | POINT (7092745.018 832780.612) |
| 2 | John C. Munro Hamilton International Airport | medium_airport | 43.173599 | -79.934998 | 780.0 | Ontario | Hamilton | POINT (7190253.037 866321.421) |
| 3 | Regina International Airport | medium_airport | 50.431900 | -104.666000 | 1894.0 | Saskatchewan | Regina | POINT (5301503.877 1678527.853) |
| 4 | Kelowna International Airport | medium_airport | 49.956100 | -119.377998 | 1421.0 | British Columbia | Kelowna | POINT (4293620.839 1953961.073) |
# distancia entre 'Billy Bishop Toronto City Centre Airport' y 'Port Severn' en km
mediumAirports.iloc[0].geometry.distance(seaports_canada_3347.iloc[0].geometry)/1000
#dividimos entre 1000 porque inicialmente está en m
135.6104388383299
#vemos las distancias entre aeropuertos medianos y puertos marítimos
seaports_canada_3347.geometry.apply\
(lambda g: mediumAirports.geometry.distance(g)/1000)
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ... | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 135.610439 | 232.693306 | 185.644901 | 1973.916368 | 3017.659979 | 2141.372100 | 261.199449 | 161.389172 | 463.675998 | 843.923902 | ... | 672.827721 | 2015.704293 | 3784.172680 | 3363.392908 | 3117.863858 | 1905.057341 | 3449.642045 | 1052.304577 | 1695.562604 | 1912.434716 |
| 1 | 1556.274240 | 1716.866003 | 1616.200857 | 3261.303406 | 4275.322811 | 3361.682116 | 1316.679831 | 1639.264816 | 1078.431202 | 2202.901525 | ... | 1942.228546 | 2826.132876 | 4637.212995 | 4173.577661 | 3876.283103 | 1730.707444 | 4706.769212 | 2364.522918 | 2775.432149 | 2905.497660 |
| 2 | 466.663608 | 425.689634 | 472.049048 | 1605.465895 | 2649.099783 | 1775.230503 | 624.405659 | 423.183645 | 774.789325 | 477.295214 | ... | 350.191394 | 1716.860673 | 3447.136013 | 3036.328153 | 2802.307151 | 1862.786618 | 3081.113145 | 687.472114 | 1352.659085 | 1579.602875 |
| 3 | 346.483837 | 338.470004 | 363.013567 | 1736.714407 | 2780.806845 | 1903.657619 | 491.301072 | 316.504561 | 647.197217 | 606.180289 | ... | 447.592670 | 1808.547188 | 3558.498158 | 3142.616415 | 2902.944422 | 1849.262280 | 3213.131021 | 814.672009 | 1466.168947 | 1687.923523 |
| 4 | 1303.410026 | 1462.716741 | 1369.290741 | 2862.557972 | 3868.148208 | 2955.079293 | 1073.880754 | 1382.113192 | 812.717891 | 1832.464993 | ... | 1567.255831 | 2408.296538 | 4222.073149 | 3759.187349 | 3463.343130 | 1382.287815 | 4298.337480 | 1981.635666 | 2362.893231 | 2488.739756 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 257 | 3588.511548 | 3499.102925 | 3577.397992 | 1524.956305 | 484.154694 | 1377.426067 | 3750.307427 | 3528.807083 | 3844.819908 | 2656.793790 | ... | 2870.866972 | 2069.754129 | 1394.799545 | 1484.039556 | 1655.063581 | 3580.873591 | 120.207673 | 2453.449910 | 1957.771472 | 1865.284028 |
| 258 | 3663.313909 | 3578.774374 | 3654.619321 | 1598.656539 | 579.921832 | 1434.753308 | 3818.709279 | 3605.752194 | 3904.458356 | 2726.295561 | ... | 2932.406230 | 2066.250078 | 1262.169996 | 1381.524595 | 1572.042886 | 3573.597409 | 270.254692 | 2520.231889 | 1993.598904 | 1883.706909 |
| 259 | 3622.416124 | 3534.222113 | 3611.912607 | 1557.983724 | 521.145783 | 1405.944139 | 3782.590220 | 3563.246252 | 3874.814932 | 2689.178617 | ... | 2901.207513 | 2081.182897 | 1360.793896 | 1462.151750 | 1640.320626 | 3591.639099 | 165.561818 | 2485.071606 | 1980.714667 | 1883.142990 |
| 260 | 3638.183724 | 3552.926509 | 3629.132640 | 1573.300311 | 550.793529 | 1411.769382 | 3794.556670 | 3580.301961 | 3881.685737 | 2701.852514 | ... | 2909.223195 | 2054.408365 | 1282.371709 | 1392.329529 | 1577.737985 | 3562.869328 | 241.234295 | 2496.166581 | 1974.440831 | 1867.606961 |
| 261 | 1581.441802 | 1741.710492 | 1640.559101 | 3301.970828 | 4317.283230 | 3403.610935 | 1341.721396 | 1664.636286 | 1107.012344 | 2240.069469 | ... | 1980.141221 | 2871.323154 | 4682.783961 | 4219.187389 | 3921.942616 | 1774.066266 | 4748.937231 | 2403.189967 | 2818.514744 | 2949.587031 |
262 rows × 329 columns
#Renombramos el encabezado 'name' por 'Aeropuertos_medianos'
mediumAirports.rename(columns={'name':'Aeropuertos_medianos'},inplace=True)
# Eliminamos duplicados en el DataFrame mediumAirports
mediumAirports = mediumAirports.drop_duplicates(subset='Aeropuertos_medianos').reset_index(drop=True)
# Obtener las distancias entre las puertos y los aeropuertos medianos con sus respectivos nombres
seaports_canada_3347.set_index('PortName').geometry.apply\
(lambda g: mediumAirports.set_index('Aeropuertos_medianos').geometry.distance(g)/1000)
C:\Users\USUARIO}\AppData\Local\Temp\ipykernel_21664\946359605.py:2: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
mediumAirports.rename(columns={'name':'Aeropuertos_medianos'},inplace=True)
| Aeropuertos_medianos | Billy Bishop Toronto City Centre Airport | London Airport | John C. Munro Hamilton International Airport | Regina International Airport | Kelowna International Airport | Saskatoon John G. Diefenbaker International Airport | Kingston Norman Rogers Airport | Waterloo Airport | Montreal International (Mirabel) Airport | Thunder Bay Airport | ... | Manitouwadge Airport | Nejanilini Lake Airport | Ross River Airport | Wrigley Airport | Rae Lakes Airport | Quaqtaq Airport | Bamfield Airstrip | Ignace Municipal Airport | Jenpeg Airport | Leaf Rapids Airport |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| PortName | |||||||||||||||||||||
| Port Severn | 135.610439 | 232.693306 | 185.644901 | 1973.916368 | 3017.659979 | 2141.372100 | 261.199449 | 161.389172 | 463.675998 | 843.923902 | ... | 672.827721 | 2015.704293 | 3784.172680 | 3363.392908 | 3117.863858 | 1905.057341 | 3449.642045 | 1052.304577 | 1695.562604 | 1912.434716 |
| Little Bras D Or | 1556.274240 | 1716.866003 | 1616.200857 | 3261.303406 | 4275.322811 | 3361.682116 | 1316.679831 | 1639.264816 | 1078.431202 | 2202.901525 | ... | 1942.228546 | 2826.132876 | 4637.212995 | 4173.577661 | 3876.283103 | 1730.707444 | 4706.769212 | 2364.522918 | 2775.432149 | 2905.497660 |
| Hilton | 466.663608 | 425.689634 | 472.049048 | 1605.465895 | 2649.099783 | 1775.230503 | 624.405659 | 423.183645 | 774.789325 | 477.295214 | ... | 350.191394 | 1716.860673 | 3447.136013 | 3036.328153 | 2802.307151 | 1862.786618 | 3081.113145 | 687.472114 | 1352.659085 | 1579.602875 |
| Kagawong | 346.483837 | 338.470004 | 363.013567 | 1736.714407 | 2780.806845 | 1903.657619 | 491.301072 | 316.504561 | 647.197217 | 606.180289 | ... | 447.592670 | 1808.547188 | 3558.498158 | 3142.616415 | 2902.944422 | 1849.262280 | 3213.131021 | 814.672009 | 1466.168947 | 1687.923523 |
| Douglastown | 1303.410026 | 1462.716741 | 1369.290741 | 2862.557972 | 3868.148208 | 2955.079293 | 1073.880754 | 1382.113192 | 812.717891 | 1832.464993 | ... | 1567.255831 | 2408.296538 | 4222.073149 | 3759.187349 | 3463.343130 | 1382.287815 | 4298.337480 | 1981.635666 | 2362.893231 | 2488.739756 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| Gold River | 3588.511548 | 3499.102925 | 3577.397992 | 1524.956305 | 484.154694 | 1377.426067 | 3750.307427 | 3528.807083 | 3844.819908 | 2656.793790 | ... | 2870.866972 | 2069.754129 | 1394.799545 | 1484.039556 | 1655.063581 | 3580.873591 | 120.207673 | 2453.449910 | 1957.771472 | 1865.284028 |
| Port Hardy | 3663.313909 | 3578.774374 | 3654.619321 | 1598.656539 | 579.921832 | 1434.753308 | 3818.709279 | 3605.752194 | 3904.458356 | 2726.295561 | ... | 2932.406230 | 2066.250078 | 1262.169996 | 1381.524595 | 1572.042886 | 3573.597409 | 270.254692 | 2520.231889 | 1993.598904 | 1883.706909 |
| Tahsis | 3622.416124 | 3534.222113 | 3611.912607 | 1557.983724 | 521.145783 | 1405.944139 | 3782.590220 | 3563.246252 | 3874.814932 | 2689.178617 | ... | 2901.207513 | 2081.182897 | 1360.793896 | 1462.151750 | 1640.320626 | 3591.639099 | 165.561818 | 2485.071606 | 1980.714667 | 1883.142990 |
| Port Mcneill | 3638.183724 | 3552.926509 | 3629.132640 | 1573.300311 | 550.793529 | 1411.769382 | 3794.556670 | 3580.301961 | 3881.685737 | 2701.852514 | ... | 2909.223195 | 2054.408365 | 1282.371709 | 1392.329529 | 1577.737985 | 3562.869328 | 241.234295 | 2496.166581 | 1974.440831 | 1867.606961 |
| Louisbourg | 1581.441802 | 1741.710492 | 1640.559101 | 3301.970828 | 4317.283230 | 3403.610935 | 1341.721396 | 1664.636286 | 1107.012344 | 2240.069469 | ... | 1980.141221 | 2871.323154 | 4682.783961 | 4219.187389 | 3921.942616 | 1774.066266 | 4748.937231 | 2403.189967 | 2818.514744 | 2949.587031 |
262 rows × 328 columns
#ordenamos los nombres en orden alfabético
seaports_canada_3347.set_index('PortName').geometry.apply\
(lambda g: mediumAirports.set_index('Aeropuertos_medianos').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)
| Aeropuertos_medianos | Aklavik/Freddie Carmichael Airport | Akulivik Airport | Alert Airport | Alert Bay Airport | Alma Airport | Amos/Magny Airport | Anahim Lake Airport | Armstrong Airport | Arviat Airport | Atikokan Municipal Airport | ... | Williams Lake Airport | Windsor Airport | Winnipeg / St. Andrews Airport | Wollaston Lake Airport | Wrigley Airport | Yarmouth Airport | Yellowknife International Airport | York Landing Airport | Yorkton Municipal Airport | Îles-de-la-Madeleine Airport |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| PortName | |||||||||||||||||||||
| Ahousat | 2118.105402 | 3162.350251 | 4181.971844 | 156.260924 | 3872.387036 | 3424.108627 | 354.112126 | 2632.228690 | 2341.511566 | 2487.427678 | ... | 426.472655 | 3418.339354 | 2074.112814 | 1761.173172 | 1527.969853 | 4510.890298 | 1600.536763 | 2113.054148 | 1679.110495 | 4579.766312 |
| Alert Bay | 1965.676638 | 3110.988478 | 4053.126228 | 1.226883 | 3874.305269 | 3434.806827 | 234.498174 | 2647.335315 | 2282.725097 | 2515.788200 | ... | 380.088512 | 3465.027512 | 2100.374016 | 1713.512682 | 1391.008086 | 4521.354827 | 1491.865428 | 2089.496999 | 1699.668283 | 4572.067337 |
| Alliford Bay | 1635.579485 | 3190.928669 | 3843.296462 | 450.762298 | 4090.148027 | 3674.978180 | 451.928227 | 2905.646385 | 2355.238497 | 2804.534030 | ... | 671.663495 | 3783.432210 | 2390.856942 | 1836.417156 | 1188.808524 | 4754.110102 | 1420.633591 | 2265.182645 | 1987.039180 | 4759.605242 |
| Amherstburg | 4124.453267 | 2098.041060 | 4525.914679 | 3465.130698 | 1162.763489 | 825.529171 | 3346.015606 | 1026.634302 | 2225.468039 | 1012.487239 | ... | 3126.340860 | 23.596361 | 1407.292630 | 2262.996418 | 3461.802376 | 1435.780997 | 3028.860019 | 1817.393595 | 1805.386040 | 1812.307205 |
| Argentia | 4770.742168 | 2126.021108 | 3885.070940 | 5066.038587 | 1328.517338 | 1815.326148 | 4871.567912 | 2556.402867 | 2913.373650 | 2782.363226 | ... | 4686.964929 | 2381.124343 | 3127.676581 | 3388.812197 | 4386.327162 | 1034.012472 | 3958.548572 | 2977.894840 | 3463.503100 | 592.575437 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| Whycocomagh | 4616.551179 | 1964.243599 | 4024.668717 | 4704.123671 | 849.355333 | 1332.236213 | 4522.702177 | 2112.035009 | 2669.197329 | 2317.552925 | ... | 4327.113582 | 1823.730567 | 2689.917057 | 3078.646595 | 4158.060174 | 466.003903 | 3715.277584 | 2636.284547 | 3051.130417 | 171.111331 |
| Wiarton | 3934.629596 | 1780.299462 | 4213.591392 | 3438.842074 | 845.636563 | 482.604987 | 3300.055284 | 854.255020 | 1985.613636 | 925.921589 | ... | 3084.176916 | 319.676976 | 1341.525827 | 2099.249698 | 3301.438696 | 1227.585960 | 2858.280174 | 1634.756255 | 1745.282261 | 1538.679576 |
| Windsor | 4104.895145 | 2072.691566 | 4500.746764 | 3455.708921 | 1141.645918 | 800.711931 | 3335.211188 | 1006.263742 | 2202.951576 | 996.673725 | ... | 3115.715466 | 8.115330 | 1393.703131 | 2244.412514 | 3443.812971 | 1423.863680 | 3010.176403 | 1797.517101 | 1792.716460 | 1795.490964 |
| Windsor | 4589.838889 | 1964.583561 | 4134.051298 | 4571.037616 | 698.567595 | 1155.079932 | 4397.293050 | 1947.817042 | 2613.365641 | 2137.669283 | ... | 4196.553344 | 1578.869949 | 2523.976709 | 2982.842031 | 4097.173643 | 206.136168 | 3649.577478 | 2527.686183 | 2897.771979 | 329.619512 |
| Wolfe Island | 4175.236052 | 1835.405221 | 4253.427393 | 3795.877688 | 610.998260 | 510.398661 | 3648.870623 | 1167.840361 | 2188.137267 | 1283.037055 | ... | 3435.549702 | 586.051156 | 1697.159336 | 2381.392241 | 3574.370678 | 848.216260 | 3125.438553 | 1909.235096 | 2097.645329 | 1207.318914 |
262 rows × 328 columns
#guardamos la matriz de distancias de los puertos y aeropuertos medianos en km
distanceMatrixKM_sea_air= seaports_canada_3347.set_index('PortName').geometry.apply\
(lambda g: mediumAirports.set_index('Aeropuertos_medianos').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)
# Distancia promedio de un puerto a los aeropuerto medianos, ordenados ascendentemente
distanceMatrixKM_sea_air.mean(axis=1).sort_values(ascending=True)
PortName
Churchill 1488.305162
Heron Bay 1586.345209
Thunder Bay 1591.871819
Michipicoten 1627.356443
Quebec Harbour 1635.701962
...
Bay Roberts 2906.577286
Holyrood 2924.930476
Long Pond 2927.608871
St John's 2941.784977
Bay Bulls 2948.764156
Length: 262, dtype: float64
# distancia promedio, mínima y máxima
SomeStats=pd.DataFrame()
SomeStats['mean']=distanceMatrixKM_sea_air.mean(axis=1)
SomeStats['min']=distanceMatrixKM_sea_air.min(axis=1)
SomeStats['max']=distanceMatrixKM_sea_air.max(axis=1)
SomeStats.head()
| mean | min | max | |
|---|---|---|---|
| PortName | |||
| Ahousat | 2523.881524 | 31.003389 | 4947.682926 |
| Alert Bay | 2497.527681 | 1.226883 | 4925.767129 |
| Alliford Bay | 2645.322585 | 11.885680 | 5074.135497 |
| Amherstburg | 2043.619403 | 23.596361 | 4525.914679 |
| Argentia | 2879.355770 | 187.730069 | 5229.645839 |
# aeropuerto mediano más lejano a cada puerto marítimo
distanceMatrixKM_sea_air.idxmax(axis="columns")
PortName
Ahousat Gander International Airport / CFB Gander
Alert Bay Gander International Airport / CFB Gander
Alliford Bay Gander International Airport / CFB Gander
Amherstburg Alert Airport
Argentia Beaver Creek Airport
...
Whycocomagh Beaver Creek Airport
Wiarton Alert Airport
Windsor Alert Airport
Windsor Beaver Creek Airport
Wolfe Island Beaver Creek Airport
Length: 262, dtype: object
# Puerto marítimo más lejano a cada aeropuerto mediano
distanceMatrixKM_sea_air.idxmax(axis="rows")
Aeropuertos_medianos
Aklavik/Freddie Carmichael Airport Bay Bulls
Akulivik Airport Queen Charlotte
Alert Airport Pelee I West Dock
Alert Bay Airport Bay Bulls
Alma Airport Queen Charlotte
...
Yarmouth Airport Queen Charlotte
Yellowknife International Airport Bay Bulls
York Landing Airport Bay Bulls
Yorkton Municipal Airport Bay Bulls
Îles-de-la-Madeleine Airport Queen Charlotte
Length: 328, dtype: object
#aeropuerto mediano más cercano a cada puerto marítimo
distanceMatrixKM_sea_air.idxmin(axis="columns")
PortName
Ahousat Tofino / Long Beach Airport
Alert Bay Alert Bay Airport
Alliford Bay Sandspit Airport
Amherstburg Windsor Airport
Argentia Gander International Airport / CFB Gander
...
Whycocomagh Port Hawkesbury Airport
Wiarton Wiarton Airport
Windsor Windsor Airport
Windsor CFB Greenwood
Wolfe Island Kingston Norman Rogers Airport
Length: 262, dtype: object
# puerto marítimo más cercano a cada aeropuerto mediano
distanceMatrixKM_sea_air.idxmin(axis="rows")
Aeropuertos_medianos
Aklavik/Freddie Carmichael Airport Tuktoyaktuk
Akulivik Airport Deception Bay
Alert Airport Resolute Bay
Alert Bay Airport Alert Bay
Alma Airport Chicoutimi (Port Saguenay)
...
Yarmouth Airport Digby
Yellowknife International Airport Coppermine
York Landing Airport Churchill
Yorkton Municipal Airport Churchill
Îles-de-la-Madeleine Airport Georgetown
Length: 328, dtype: object
#Ejercicio 2
rivers
| NOMBRE | CUENCA | geometry | |
|---|---|---|---|
| 0 | Ajaqutalik | NaN | LINESTRING (6464581.465 3481723.236, 6462702.0... |
| 1 | Albany | NaN | MULTILINESTRING ((6711064.630 1691721.838, 672... |
| 2 | Amadjuak | NaN | MULTILINESTRING ((7090849.549 3362460.388, 709... |
| 3 | Assinibaine | Nelson/Saskatchewan | LINESTRING (5647695.689 1535314.231, 5653589.2... |
| 4 | Athabasca | Mackenzie | LINESTRING (4506990.720 2127228.127, 4512692.3... |
| ... | ... | ... | ... |
| 70 | Sungukpagaluk | NaN | LINESTRING (5229681.645 4121173.738, 5230373.0... |
| 71 | Thelon | NaN | MULTILINESTRING ((5814318.875 3151150.291, 581... |
| 72 | Thlewiaza | NaN | MULTILINESTRING ((5658280.765 2671464.848, 565... |
| 73 | Waterhen | Nelson/Saskatchewan | LINESTRING (5677129.239 1764643.320, 5676594.1... |
| 74 | Yukon | Yukon | MULTILINESTRING ((4097626.962 3817121.037, 409... |
75 rows × 3 columns
#renombramos
rivers.rename(columns={'NOMBRE':'Rivers_name'},inplace=True)
#vemos la lista de los ríos
rivers['Rivers_name'].values.tolist()
['Ajaqutalik', 'Albany', 'Amadjuak', 'Assinibaine', 'Athabasca', 'Atikonak', 'Attawapiskat', 'Back', 'Burnside', 'Caniapiscau', 'Canoe', 'Churchill', 'Churchill', 'Columbia', 'Coppermine', 'Dauphin', 'Dubawnt', 'Feuilles', 'Finlay', 'Flathead', 'Fraser', 'Gander', 'George', 'Grand Baleine', 'Great Bear', 'Harricana', 'Hayes', 'Hayes', 'Hone', 'Kechika', 'Kenogami', 'Kepimits', 'Koukdjuak', 'Kunjjua', 'Liard', 'Mackenzie', 'Macmillian', 'Manicouragan', 'Mattagami', 'Melezes', 'Missinaibi', 'Mistassibi', 'Nechako', 'Nelson', 'Niagara', 'Nipigon', 'North Saskatchewan', 'Nottaway', 'Ottawa', 'Parsnip', 'Peace', 'Peel', 'Pend Oreille', 'Peribonca', 'Porcupine', 'Rainy', 'Red Deer', 'Red River of the North', 'Richelieu', 'Rupert', 'Saguenay', 'Saskatchewan', 'Severn', 'Slave', 'Souris', 'South Saskatchewan', 'St. Clair', 'St. Johns', 'St. Lawrence', 'Stikine', 'Sungukpagaluk', 'Thelon', 'Thlewiaza', 'Waterhen', 'Yukon']
#buscamos un río que contenga la palabra "Macmillian"
rivers[rivers.Rivers_name.str.contains('Macmillian')]
| Rivers_name | CUENCA | geometry | |
|---|---|---|---|
| 36 | Macmillian | Yukon | LINESTRING (4463169.308 3595546.764, 4460953.2... |
#distancia entre el río Macmillian y los aeropuertos medianos
rivers[rivers.Rivers_name.str.contains('Macmillian')].iloc[0].geometry.distance(mediumAirports.geometry)
0 3.827133e+06
1 3.799406e+06
2 3.844028e+06
3 2.070797e+06
4 1.614516e+06
...
323 2.949561e+06
324 1.623627e+06
325 2.730280e+06
326 2.028070e+06
327 1.795451e+06
Name: geometry, Length: 328, dtype: float64
#guardamos la matriz de la distancia entre ríos y aeropuertos medianos
distanceMatrixKM_riv_air=rivers.set_index('Rivers_name').geometry.apply\
(lambda g: mediumAirports.set_index('Aeropuertos_medianos').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)
distanceMatrixKM_riv_air
| Aeropuertos_medianos | Aklavik/Freddie Carmichael Airport | Akulivik Airport | Alert Airport | Alert Bay Airport | Alma Airport | Amos/Magny Airport | Anahim Lake Airport | Armstrong Airport | Arviat Airport | Atikokan Municipal Airport | ... | Williams Lake Airport | Windsor Airport | Winnipeg / St. Andrews Airport | Wollaston Lake Airport | Wrigley Airport | Yarmouth Airport | Yellowknife International Airport | York Landing Airport | Yorkton Municipal Airport | Îles-de-la-Madeleine Airport |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Rivers_name | |||||||||||||||||||||
| Ajaqutalik | 1963.596627 | 804.551109 | 1649.033602 | 2878.223032 | 2210.430556 | 2117.890157 | 2645.535952 | 1905.493199 | 821.220110 | 2092.584265 | ... | 2544.908641 | 2797.916673 | 2022.041229 | 1349.756601 | 1745.334916 | 2823.603648 | 1423.283201 | 1367.914813 | 2005.055385 | 2534.987143 |
| Albany | 2814.886285 | 960.474062 | 3385.058559 | 2371.137971 | 831.589147 | 478.292853 | 2206.578604 | 112.822777 | 999.517451 | 251.326387 | ... | 1999.972474 | 998.640931 | 391.401331 | 954.108019 | 2155.906199 | 1503.014755 | 1719.328076 | 512.659256 | 701.150159 | 1528.460561 |
| Amadjuak | 2585.772815 | 520.769754 | 1851.652355 | 3400.934780 | 1754.063730 | 1793.471523 | 3173.465207 | 1845.450588 | 1149.633669 | 2082.065755 | ... | 3045.895953 | 2562.101797 | 2146.192979 | 1744.138375 | 2368.078761 | 2295.370692 | 2011.170370 | 1581.834073 | 2252.754908 | 1943.154268 |
| Assinibaine | 2763.072562 | 1667.333035 | 3755.719461 | 1936.339055 | 1848.403040 | 1375.124722 | 1805.478144 | 585.665583 | 1232.499418 | 415.314528 | ... | 1587.024154 | 1395.781299 | 20.609774 | 945.264258 | 2041.247972 | 2454.166483 | 1658.037516 | 677.594049 | 271.723871 | 2586.737826 |
| Athabasca | 1541.020666 | 1801.086403 | 2991.570978 | 653.240797 | 2743.649303 | 2357.858930 | 484.237555 | 1619.890646 | 966.146546 | 1572.100120 | ... | 276.178974 | 2576.573829 | 1179.247657 | 452.470236 | 814.285029 | 3420.497087 | 462.988160 | 916.132025 | 789.552377 | 3383.087644 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| Sungukpagaluk | 585.211763 | 2144.998788 | 1729.002529 | 2307.836663 | 3502.836829 | 3288.346341 | 2093.655885 | 2804.460346 | 1612.699894 | 2891.043794 | ... | 2113.831333 | 3810.968318 | 2629.861885 | 1667.357724 | 919.024742 | 4162.734269 | 1032.069560 | 2033.236916 | 2392.472502 | 3939.217400 |
| Thelon | 1370.163244 | 851.983782 | 2165.067857 | 1761.852778 | 2135.129661 | 1917.360331 | 1529.634819 | 1518.392673 | 307.000197 | 1661.928994 | ... | 1430.245077 | 2490.303023 | 1423.583903 | 474.362429 | 803.395689 | 2803.563620 | 368.133657 | 832.651218 | 1222.823404 | 2624.899515 |
| Thlewiaza | 1703.688397 | 882.480445 | 2594.529627 | 1819.308734 | 1954.273487 | 1667.926833 | 1596.932454 | 1172.239932 | 75.194678 | 1295.261012 | ... | 1457.591855 | 2168.331357 | 1067.545115 | 196.260238 | 1102.153511 | 2635.304723 | 650.808637 | 456.287424 | 912.001717 | 2525.300982 |
| Waterhen | 2380.880335 | 1532.802871 | 3438.024587 | 1769.465878 | 2000.988733 | 1546.939984 | 1603.269875 | 756.153057 | 941.392527 | 647.508289 | ... | 1396.991492 | 1645.780773 | 249.421138 | 556.915273 | 1671.382247 | 2633.704897 | 1271.635553 | 413.347043 | 173.686938 | 2718.465602 |
| Yukon | 459.879258 | 2794.828822 | 2780.129868 | 966.831225 | 3860.323326 | 3492.797898 | 808.880264 | 2777.078083 | 1981.890622 | 2732.503186 | ... | 938.229139 | 3736.649610 | 2342.236041 | 1571.052010 | 573.483643 | 4539.787435 | 946.110267 | 2040.606851 | 1961.105029 | 4476.455435 |
75 rows × 328 columns
# distancia entre el río seleccionado y cada aeropuerto mediano ordenado ascendentemente
distanceMatrixKM_riv_air.loc['Macmillian'].sort_values()
Aeropuertos_medianos
Mayo Airport 3.343342
Dawson City Airport 78.574139
Beaver Creek Airport 120.684409
Faro Airport 122.743980
Ross River Airport 146.828252
...
CFB Greenwood 4395.788641
Yarmouth Airport 4445.843425
Port Hawkesbury Airport 4503.858910
Sydney / J.A. Douglas McCurdy Airport 4520.913344
Gander International Airport / CFB Gander 4531.545108
Name: Macmillian, Length: 328, dtype: float64
#creamos el mapa iterativo del río Macmillian y los aeropuertos medianos
base=rivers[rivers.Rivers_name.str.contains('Macmillian')].explore()
mediumAirports.explore(m=base,color='deeppink',marker_kwds=dict(radius=10))
#observamos las columnas de los ríos
rivers.columns
Index(['Rivers_name', 'CUENCA', 'geometry'], dtype='object')
#Eliminamos los valores nulos de la columna de cuenca
rivers[~rivers.CUENCA.isna()]
| Rivers_name | CUENCA | geometry | |
|---|---|---|---|
| 3 | Assinibaine | Nelson/Saskatchewan | LINESTRING (5647695.689 1535314.231, 5653589.2... |
| 4 | Athabasca | Mackenzie | LINESTRING (4506990.720 2127228.127, 4512692.3... |
| 10 | Canoe | Columbia | LINESTRING (4408161.378 2244043.229, 4407445.9... |
| 11 | Churchill | Churchill | MULTILINESTRING ((5841167.230 2364958.406, 584... |
| 13 | Columbia | Columbia | LINESTRING (4538186.876 1888850.101, 4539161.6... |
| 15 | Dauphin | Nelson/Saskatchewan | MULTILINESTRING ((5780372.975 1776876.296, 577... |
| 18 | Finlay | Mackenzie | LINESTRING (4182403.062 2856380.024, 4182955.2... |
| 19 | Flathead | Columbia | LINESTRING (4574707.400 1775959.210, 4580037.9... |
| 24 | Great Bear | Mackenzie | MULTILINESTRING ((4820694.516 3539848.271, 480... |
| 29 | Kechika | Mackenzie | LINESTRING (4312642.732 2918508.864, 4314261.9... |
| 34 | Liard | Mackenzie | MULTILINESTRING ((4278805.685 3391429.082, 426... |
| 35 | Mackenzie | Mackenzie | MULTILINESTRING ((4568301.856 4031042.084, 456... |
| 36 | Macmillian | Yukon | LINESTRING (4463169.308 3595546.764, 4460953.2... |
| 43 | Nelson | Nelson/Saskatchewan | MULTILINESTRING ((5808367.216 2044799.150, 580... |
| 44 | Niagara | St. Lawrence | MULTILINESTRING ((7279027.524 853236.309, 7279... |
| 45 | Nipigon | St. Lawrence | LINESTRING (6462809.963 1485230.012, 6459682.8... |
| 46 | North Saskatchewan | Nelson/Saskatchewan | LINESTRING (4521710.900 2104299.029, 4528009.0... |
| 48 | Ottawa | St. Lawrence | LINESTRING (7048147.776 1418439.567, 7050483.5... |
| 49 | Parsnip | Mackenzie | LINESTRING (4367970.957 2468843.980, 4356038.4... |
| 50 | Peace | Mackenzie | MULTILINESTRING ((4316242.983 2780170.573, 431... |
| 51 | Peel | Mackenzie | LINESTRING (4474139.627 3664637.587, 4471264.4... |
| 52 | Pend Oreille | Columbia | LINESTRING (4366808.676 1807257.798, 4383467.3... |
| 54 | Porcupine | Yukon | LINESTRING (4231968.743 4019343.940, 4241753.4... |
| 55 | Rainy | St. Lawrence | LINESTRING (5993259.169 1417838.409, 6001449.9... |
| 56 | Red Deer | Nelson/Saskatchewan | LINESTRING (4583716.243 2019766.224, 4583719.4... |
| 57 | Red River of the North | Nelson/Saskatchewan | MULTILINESTRING ((5825031.458 1541780.913, 582... |
| 58 | Richelieu | St. Lawrence | LINESTRING (7673605.654 1198916.768, 7664175.0... |
| 61 | Saskatchewan | Nelson/Saskatchewan | MULTILINESTRING ((5708994.998 1916565.984, 570... |
| 63 | Slave | Mackenzie | MULTILINESTRING ((5110275.427 2684305.779, 511... |
| 64 | Souris | Nelson/Saskatchewan | LINESTRING (5372486.351 1616433.223, 5364747.1... |
| 65 | South Saskatchewan | Nelson/Saskatchewan | MULTILINESTRING ((5172258.721 1800983.675, 516... |
| 66 | St. Clair | St. Lawrence | MULTILINESTRING ((6374981.548 1320978.713, 638... |
| 68 | St. Lawrence | St. Lawrence | MULTILINESTRING ((7759488.644 1431046.964, 773... |
| 73 | Waterhen | Nelson/Saskatchewan | LINESTRING (5677129.239 1764643.320, 5676594.1... |
| 74 | Yukon | Yukon | MULTILINESTRING ((4097626.962 3817121.037, 409... |
#unimos la geometría de los ríos que cuentan con la misma cuenca
cuencas=rivers.dissolve(by='CUENCA')
cuencas
| geometry | Rivers_name | |
|---|---|---|
| CUENCA | ||
| Churchill | MULTILINESTRING ((5841167.230 2364958.406, 584... | Churchill |
| Columbia | MULTILINESTRING ((4408161.378 2244043.229, 440... | Canoe |
| Mackenzie | MULTILINESTRING ((4506990.720 2127228.127, 451... | Athabasca |
| Nelson/Saskatchewan | MULTILINESTRING ((5647695.689 1535314.231, 565... | Assinibaine |
| St. Lawrence | MULTILINESTRING ((7279027.524 853236.309, 7279... | Niagara |
| Yukon | MULTILINESTRING ((4463169.308 3595546.764, 446... | Macmillian |
#creamos una nueva columna "Cuenca" que contendrá las cuencas unificadas
cuencas['CUENCAS']=cuencas.index
cuencas.reset_index(drop=True,inplace=True)
cuencas
| geometry | Rivers_name | CUENCAS | |
|---|---|---|---|
| 0 | MULTILINESTRING ((5841167.230 2364958.406, 584... | Churchill | Churchill |
| 1 | MULTILINESTRING ((4408161.378 2244043.229, 440... | Canoe | Columbia |
| 2 | MULTILINESTRING ((4506990.720 2127228.127, 451... | Athabasca | Mackenzie |
| 3 | MULTILINESTRING ((5647695.689 1535314.231, 565... | Assinibaine | Nelson/Saskatchewan |
| 4 | MULTILINESTRING ((7279027.524 853236.309, 7279... | Niagara | St. Lawrence |
| 5 | MULTILINESTRING ((4463169.308 3595546.764, 446... | Macmillian | Yukon |
#creamos una matriz para las distancias entre cuencas y aeropuertos medianos
distanceMatrixKM_cue_air=cuencas.set_index('CUENCAS').geometry.apply\
(lambda g: mediumAirports.set_index('Aeropuertos_medianos').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)
distanceMatrixKM_cue_air
| Aeropuertos_medianos | Aklavik/Freddie Carmichael Airport | Akulivik Airport | Alert Airport | Alert Bay Airport | Alma Airport | Amos/Magny Airport | Anahim Lake Airport | Armstrong Airport | Arviat Airport | Atikokan Municipal Airport | ... | Williams Lake Airport | Windsor Airport | Winnipeg / St. Andrews Airport | Wollaston Lake Airport | Wrigley Airport | Yarmouth Airport | Yellowknife International Airport | York Landing Airport | Yorkton Municipal Airport | Îles-de-la-Madeleine Airport |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CUENCAS | |||||||||||||||||||||
| Churchill | 1775.833787 | 932.025025 | 2832.039657 | 1037.232025 | 1817.179020 | 1488.147348 | 848.353557 | 879.150078 | 310.457898 | 961.930267 | ... | 658.141658 | 1892.281756 | 670.955472 | 52.089642 | 1049.303255 | 2498.865089 | 686.259862 | 112.965045 | 458.758903 | 2434.122224 |
| Columbia | 1854.653197 | 2529.904372 | 3709.057578 | 553.067057 | 3086.391564 | 2622.720594 | 380.275517 | 1828.389390 | 1729.868396 | 1662.512377 | ... | 174.252429 | 2572.851481 | 1256.165019 | 1138.342971 | 1154.374652 | 3705.453476 | 1089.211536 | 1429.782651 | 880.871528 | 3811.346424 |
| Mackenzie | 1.153187 | 1506.377506 | 2223.344100 | 546.655566 | 2475.603418 | 2119.077818 | 313.540457 | 1452.226565 | 672.745539 | 1470.862191 | ... | 251.048836 | 2455.935185 | 1155.305545 | 204.218772 | 0.369399 | 3155.570807 | 7.617041 | 672.175023 | 789.552377 | 3100.023381 |
| Nelson/Saskatchewan | 1904.313057 | 926.844686 | 2960.460626 | 686.293980 | 1679.590439 | 1317.001698 | 545.065827 | 522.670750 | 456.519433 | 392.559354 | ... | 325.489170 | 1355.475939 | 4.548289 | 444.115015 | 1158.887977 | 2359.419655 | 918.292273 | 14.193694 | 157.965890 | 2318.359937 |
| St. Lawrence | 2908.947666 | 1308.538999 | 3642.953010 | 2240.711931 | 199.452232 | 140.896684 | 2100.598380 | 12.747575 | 1212.566704 | 55.046305 | ... | 1884.506622 | 4.325925 | 141.857282 | 1042.902505 | 2216.832383 | 529.355570 | 1799.811784 | 676.097777 | 545.888404 | 731.555401 |
| Yukon | 140.610124 | 2577.379607 | 2445.036827 | 966.831225 | 3763.952947 | 3442.271587 | 808.880264 | 2777.078083 | 1819.031281 | 2732.503186 | ... | 938.229139 | 3736.649610 | 2342.236041 | 1541.803786 | 341.133143 | 4445.843425 | 791.752809 | 2009.048817 | 1961.105029 | 4327.160324 |
6 rows × 328 columns
#aeropuertos medianos más cercanos a las cuencas
mins=distanceMatrixKM_cue_air.idxmin(axis="columns")
mins
CUENCAS Churchill Leaf Rapids Airport Columbia Golden Airport Mackenzie Wrigley Airport Nelson/Saskatchewan Jenpeg Airport St. Lawrence Sault Ste Marie Airport Yukon Old Crow Airport dtype: object
#seleccionamos uno de ellos
mins[1]
'Golden Airport'
#mapa interactivo
base=cuencas.explore()
#aeropuertos medianos más cercanos
mediumAirports[mediumAirports.Aeropuertos_medianos.isin(mins)].explore(m=base,color='purple',marker_kwds=dict(radius=10))
# aeropuertos medianos no cercanos
mediumAirports[~mediumAirports.Aeropuertos_medianos.isin(mins)].explore(m=base,color='green',marker_kwds=dict(radius=5))
#Ejercicio 3
#creamos un convex hull de las cuencas
cuencas.convex_hull.plot()
<Axes: >
#creamos un data frame con la geometría de los convex hull
cuencas_hulls=cuencas.convex_hull.to_frame()
#creamos una nueva columna y asignamos los nombres de cada cuenca
cuencas_hulls['CUENCAS']=['Churchill', 'Columbia', 'Mackenzie', 'Nelson/Saskatchewan', 'St. Lawrence', 'Yukon']
#renombramos
cuencas_hulls.rename(columns={0:'geometry'},inplace=True)
#geometry pasa a ser de tipo GeoSeries
cuencas_hulls=cuencas_hulls.set_geometry('geometry')
#asignamos el crs
cuencas_hulls.crs="EPSG:3347"
cuencas_hulls
| geometry | CUENCAS | |
|---|---|---|
| 0 | POLYGON ((5190336.147 2144391.424, 5135014.076... | Churchill |
| 1 | POLYGON ((4583270.507 1718811.578, 4385912.991... | Columbia |
| 2 | POLYGON ((4506990.720 2127228.127, 4182403.062... | Mackenzie |
| 3 | POLYGON ((5524938.266 1371854.738, 4644833.776... | Nelson/Saskatchewan |
| 4 | POLYGON ((6585406.313 610438.223, 6581151.688 ... | St. Lawrence |
| 5 | POLYGON ((4149683.184 3190954.529, 4136610.634... | Yukon |
#Creamos la matriz de las distancias del convex hull de las cuencas y los aeropuertos medianos
distanceMatrixKM_cueHull_air=cuencas_hulls.set_index('CUENCAS').geometry.apply\
(lambda g: mediumAirports.set_index('Aeropuertos_medianos').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)
distanceMatrixKM_cueHull_air
| Aeropuertos_medianos | Aklavik/Freddie Carmichael Airport | Akulivik Airport | Alert Airport | Alert Bay Airport | Alma Airport | Amos/Magny Airport | Anahim Lake Airport | Armstrong Airport | Arviat Airport | Atikokan Municipal Airport | ... | Williams Lake Airport | Windsor Airport | Winnipeg / St. Andrews Airport | Wollaston Lake Airport | Wrigley Airport | Yarmouth Airport | Yellowknife International Airport | York Landing Airport | Yorkton Municipal Airport | Îles-de-la-Madeleine Airport |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CUENCAS | |||||||||||||||||||||
| Churchill | 1775.833787 | 932.025025 | 2832.039657 | 1037.232025 | 1817.179020 | 1488.141389 | 848.353557 | 879.150078 | 310.457898 | 955.982453 | ... | 658.141658 | 1892.281756 | 670.955472 | 17.595399 | 1049.303255 | 2498.865089 | 686.259862 | 99.489617 | 437.089814 | 2434.122224 |
| Columbia | 1854.653197 | 2529.787737 | 3709.057578 | 551.089011 | 3086.391564 | 2622.720594 | 380.275517 | 1828.389390 | 1727.959781 | 1662.512377 | ... | 174.127812 | 2572.851481 | 1256.165019 | 1137.075507 | 1154.374652 | 3705.453476 | 1089.211536 | 1427.951611 | 880.871528 | 3811.346424 |
| Mackenzie | 0.000000 | 1506.377506 | 2220.800001 | 538.575452 | 2475.603418 | 2119.077818 | 306.214404 | 1452.226565 | 672.318169 | 1470.610763 | ... | 196.803908 | 2455.935185 | 1125.788912 | 204.218772 | 0.000000 | 3155.570807 | 0.000000 | 672.175023 | 775.158208 | 3100.023381 |
| Nelson/Saskatchewan | 1904.313057 | 926.844686 | 2960.460626 | 686.293980 | 1675.761838 | 1274.634697 | 545.065827 | 503.279711 | 456.519433 | 387.909244 | ... | 325.489170 | 1355.475939 | 0.000000 | 228.420540 | 1158.887977 | 2348.623084 | 873.345038 | 0.000000 | 0.000000 | 2318.359937 |
| St. Lawrence | 2908.947666 | 1265.960922 | 3642.953010 | 2240.711931 | 168.404864 | 0.000000 | 2100.598380 | 1.210237 | 1199.810796 | 0.000000 | ... | 1884.506622 | 0.000000 | 141.857282 | 1042.902505 | 2216.832383 | 529.355570 | 1799.811784 | 676.097777 | 545.888404 | 731.555401 |
| Yukon | 140.199096 | 2577.379607 | 2445.036827 | 966.831225 | 3763.952947 | 3442.271587 | 808.880264 | 2771.680934 | 1819.031281 | 2732.503186 | ... | 938.229139 | 3736.649610 | 2342.236041 | 1534.436202 | 341.095255 | 4445.843425 | 791.752809 | 2005.793045 | 1961.105029 | 4327.160324 |
6 rows × 328 columns
#aeropuertos medianos cercanos a cada cuenca
mins=distanceMatrixKM_cueHull_air.idxmin(axis="columns")
mins
CUENCAS Churchill Bonnyville Airport Columbia Blue River Airport Mackenzie Aklavik/Freddie Carmichael Airport Nelson/Saskatchewan Berens River Airport St. Lawrence Amos/Magny Airport Yukon Atlin Airport dtype: object
# plotemos
base=cuencas_hulls.explore()
#aeropuertos más cercanos
mediumAirports[mediumAirports.Aeropuertos_medianos.isin(mins)].explore(m=base,color='indigo',marker_kwds=dict(radius=10))
#aeropuertos no cercanos
mediumAirports[~mediumAirports.Aeropuertos_medianos.isin(mins)].explore(m=base,color='lightcoral',marker_kwds=dict(radius=5))
#Ejercicio 4
# Aplicaremos buffer, para ello vemos la matriz que hicimos
distanceMatrixKM_riv_air
| Aeropuertos_medianos | Aklavik/Freddie Carmichael Airport | Akulivik Airport | Alert Airport | Alert Bay Airport | Alma Airport | Amos/Magny Airport | Anahim Lake Airport | Armstrong Airport | Arviat Airport | Atikokan Municipal Airport | ... | Williams Lake Airport | Windsor Airport | Winnipeg / St. Andrews Airport | Wollaston Lake Airport | Wrigley Airport | Yarmouth Airport | Yellowknife International Airport | York Landing Airport | Yorkton Municipal Airport | Îles-de-la-Madeleine Airport |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Rivers_name | |||||||||||||||||||||
| Ajaqutalik | 1963.596627 | 804.551109 | 1649.033602 | 2878.223032 | 2210.430556 | 2117.890157 | 2645.535952 | 1905.493199 | 821.220110 | 2092.584265 | ... | 2544.908641 | 2797.916673 | 2022.041229 | 1349.756601 | 1745.334916 | 2823.603648 | 1423.283201 | 1367.914813 | 2005.055385 | 2534.987143 |
| Albany | 2814.886285 | 960.474062 | 3385.058559 | 2371.137971 | 831.589147 | 478.292853 | 2206.578604 | 112.822777 | 999.517451 | 251.326387 | ... | 1999.972474 | 998.640931 | 391.401331 | 954.108019 | 2155.906199 | 1503.014755 | 1719.328076 | 512.659256 | 701.150159 | 1528.460561 |
| Amadjuak | 2585.772815 | 520.769754 | 1851.652355 | 3400.934780 | 1754.063730 | 1793.471523 | 3173.465207 | 1845.450588 | 1149.633669 | 2082.065755 | ... | 3045.895953 | 2562.101797 | 2146.192979 | 1744.138375 | 2368.078761 | 2295.370692 | 2011.170370 | 1581.834073 | 2252.754908 | 1943.154268 |
| Assinibaine | 2763.072562 | 1667.333035 | 3755.719461 | 1936.339055 | 1848.403040 | 1375.124722 | 1805.478144 | 585.665583 | 1232.499418 | 415.314528 | ... | 1587.024154 | 1395.781299 | 20.609774 | 945.264258 | 2041.247972 | 2454.166483 | 1658.037516 | 677.594049 | 271.723871 | 2586.737826 |
| Athabasca | 1541.020666 | 1801.086403 | 2991.570978 | 653.240797 | 2743.649303 | 2357.858930 | 484.237555 | 1619.890646 | 966.146546 | 1572.100120 | ... | 276.178974 | 2576.573829 | 1179.247657 | 452.470236 | 814.285029 | 3420.497087 | 462.988160 | 916.132025 | 789.552377 | 3383.087644 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| Sungukpagaluk | 585.211763 | 2144.998788 | 1729.002529 | 2307.836663 | 3502.836829 | 3288.346341 | 2093.655885 | 2804.460346 | 1612.699894 | 2891.043794 | ... | 2113.831333 | 3810.968318 | 2629.861885 | 1667.357724 | 919.024742 | 4162.734269 | 1032.069560 | 2033.236916 | 2392.472502 | 3939.217400 |
| Thelon | 1370.163244 | 851.983782 | 2165.067857 | 1761.852778 | 2135.129661 | 1917.360331 | 1529.634819 | 1518.392673 | 307.000197 | 1661.928994 | ... | 1430.245077 | 2490.303023 | 1423.583903 | 474.362429 | 803.395689 | 2803.563620 | 368.133657 | 832.651218 | 1222.823404 | 2624.899515 |
| Thlewiaza | 1703.688397 | 882.480445 | 2594.529627 | 1819.308734 | 1954.273487 | 1667.926833 | 1596.932454 | 1172.239932 | 75.194678 | 1295.261012 | ... | 1457.591855 | 2168.331357 | 1067.545115 | 196.260238 | 1102.153511 | 2635.304723 | 650.808637 | 456.287424 | 912.001717 | 2525.300982 |
| Waterhen | 2380.880335 | 1532.802871 | 3438.024587 | 1769.465878 | 2000.988733 | 1546.939984 | 1603.269875 | 756.153057 | 941.392527 | 647.508289 | ... | 1396.991492 | 1645.780773 | 249.421138 | 556.915273 | 1671.382247 | 2633.704897 | 1271.635553 | 413.347043 | 173.686938 | 2718.465602 |
| Yukon | 459.879258 | 2794.828822 | 2780.129868 | 966.831225 | 3860.323326 | 3492.797898 | 808.880264 | 2777.078083 | 1981.890622 | 2732.503186 | ... | 938.229139 | 3736.649610 | 2342.236041 | 1571.052010 | 573.483643 | 4539.787435 | 946.110267 | 2040.606851 | 1961.105029 | 4476.455435 |
75 rows × 328 columns
#distancia mínima entre el río Peace y cualquier aeropuerto
distanceMatrixKM_riv_air.loc['Peace'].min()
2.6755097270737505
#guardamos nuestra distancia mínima y la pasamos a metros
minMts=distanceMatrixKM_riv_air.loc['Peace'].min()*1000
#creamos un buffer que es un polígono
rivers[rivers.Rivers_name=='Peace'].buffer(distance = minMts)
50 POLYGON ((4334276.465 2603789.159, 4334337.422... dtype: geometry
# vemos el buffer del río Peace con la distancia mínima
bufferAroundPeace=rivers[rivers.Rivers_name=='Peace'].buffer(distance = minMts)
bufferAsBase=bufferAroundPeace.explore(color='lightpink')
rivers[rivers.Rivers_name=='Peace'].explore(m=bufferAsBase,color='blue',style_kwds={'weight':0.5})
#seleccionamos los aeropuertos pequeños
smallAirports=airports[airports.kind=='small_airport']
# plotting
rivers[rivers.Rivers_name=='Peace'].explore(m=bufferAsBase,color='blue',style_kwds={'weight':0.5})
smallAirports.explore(m=bufferAsBase,color='black')
#clipeamos los aeropuertos que están solo en el buffer
riversWithinBuffer=smallAirports.clip(mask=bufferAroundPeace)
riversWithinBuffer
| name | kind | latitude_deg | longitude_deg | elevation_ft | region_name | municipality | geometry | |
|---|---|---|---|---|---|---|---|---|
| 846 | Fox Lake Airport | small_airport | 58.473640 | -114.549320 | 850.0 | Alberta | Fox Lake | POINT (4937886.303 2695290.757) |
| 847 | Garden River Airport | small_airport | 58.713902 | -113.875999 | 790.0 | Alberta | Garden River | POINT (4982659.676 2706639.653) |
| 912 | Ospika Airport | small_airport | 56.275002 | -124.052002 | 2300.0 | British Columbia | Ospika | POINT (4331846.564 2711847.273) |
| 654 | Fort Grahame Airport | small_airport | 56.521388 | -124.470291 | 2230.0 | British Columbia | Fort Graham | POINT (4322874.291 2747586.305) |
| 1405 | Prairie Point Airport | small_airport | 58.272539 | -116.461299 | 990.0 | Alberta | La Crête | POINT (4828403.662 2714541.920) |
| 296 | Fort Vermilion Airport | small_airport | 58.404202 | -115.950996 | 836.0 | Alberta | Fort Vermilion | POINT (4860755.457 2716922.815) |
#mapa del buffer del río Peace y los aeropuertos pequeños que se encuentran dentro de él
bufferAsBase=bufferAroundPeace.explore(color='lightseagreen')
rivers[rivers.Rivers_name=='Peace'].explore(m=bufferAsBase,color='blue',style_kwds={'weight':0.5})
riversWithinBuffer.explore(m=bufferAsBase,color='black')
# buscamos el valor mínimo de todas las distancias de la matriz
distanceMatrixKM_riv_air.min(axis=1).min()
0.023280358764795134
# usamos el valor calculado previamente para hacer le mapa y lo multiplicamos por 40 para que sea más grande
minMinMts_40=40*distanceMatrixKM_riv_air.min(axis=1).min()*1000
#creamos el buffer para todos los ríos
allMinBuffer=rivers.buffer(distance = minMinMts_40).explore(color='lightsalmon')
rivers.explore(m=allMinBuffer,color='blue',style_kwds={'weight':0.5})
smallAirports.explore(m=allMinBuffer,color='darkgray')
# vemos todos los buffer
rivers.buffer(distance = minMinMts_40)
0 POLYGON ((6461813.741 3487422.615, 6461790.496...
1 POLYGON ((6175851.083 1754860.587, 6192996.229...
2 POLYGON ((7089919.604 3362508.980, 7089921.955...
3 POLYGON ((5654084.168 1529725.060, 5663388.087...
4 POLYGON ((4511833.628 2129157.649, 4513311.891...
...
70 POLYGON ((5229467.177 4124294.464, 5229491.533...
71 POLYGON ((5462514.018 3022458.172, 5471182.056...
72 POLYGON ((5657370.441 2671218.137, 5655824.759...
73 POLYGON ((5671028.847 1777831.739, 5675721.791...
74 POLYGON ((4048698.145 3402462.445, 4058121.237...
Length: 75, dtype: geometry
# verificamos que sea del tipo geoserie
riversAll_buf=rivers.buffer(distance = minMinMts_40)
type(riversAll_buf)
geopandas.geoseries.GeoSeries
# formateamos, pasamos de geoseries a geodataframe
riversAll_bufDF=riversAll_buf.to_frame()
riversAll_bufDF.rename(columns={0:'geometry'},inplace=True)
riversAll_bufDF = riversAll_bufDF.set_geometry("geometry")
riversAll_bufDF.crs
<Projected CRS: EPSG:3347> Name: NAD83 / Statistics Canada Lambert Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: Canada - onshore and offshore - Alberta; British Columbia; Manitoba; New Brunswick; Newfoundland and Labrador; Northwest Territories; Nova Scotia; Nunavut; Ontario; Prince Edward Island; Quebec; Saskatchewan; Yukon. - bounds: (-141.01, 38.21, -40.73, 86.46) Coordinate Operation: - name: Statistics Canada Lambert - method: Lambert Conic Conformal (2SP) Datum: North American Datum 1983 - Ellipsoid: GRS 1980 - Prime Meridian: Greenwich
#clipeamos todos los aeropuertos dentro del buffer
allRiversWithinBuffs=smallAirports.clip(riversAll_bufDF)
allRiversWithinBuffs
| name | kind | latitude_deg | longitude_deg | elevation_ft | region_name | municipality | geometry | |
|---|---|---|---|---|---|---|---|---|
| 447 | Fairmont Hot Springs Airport | small_airport | 50.331052 | -115.873739 | 2661.0 | British Columbia | Fairmont Hot Springs | POINT (4539193.126 1893856.171) |
| 924 | Radium Hot Springs Airport | small_airport | 50.627155 | -116.094981 | 2650.0 | British Columbia | Radium Hot Springs | POINT (4536809.794 1930068.289) |
| 170 | Fort Langley Airport | small_airport | 49.167500 | -122.555000 | 30.0 | British Columbia | Fort Langley | POINT (4049230.508 1976760.212) |
| 1569 | Squaw Rapids Airport | small_airport | 53.678600 | -103.349998 | 1040.0 | Saskatchewan | Squaw Rapids | POINT (5456999.800 2012493.148) |
| 248 | Sundre Airport | small_airport | 51.774200 | -114.677002 | 3656.0 | Alberta | Sundre | POINT (4674234.561 2012570.724) |
| 438 | Red Deer Forestry Airport | small_airport | 51.651402 | -115.238998 | 4646.0 | Alberta | Clearwater | POINT (4633467.384 2013582.581) |
| 861 | Hinton / Entrance Airport | small_airport | 53.381401 | -117.700996 | 3450.0 | Alberta | Hinton | POINT (4552295.670 2251956.583) |
| 900 | McBride Airport/Charlie Leake Field | small_airport | 53.314999 | -120.170998 | 2350.0 | British Columbia | McBride | POINT (4401664.225 2312240.666) |
| 1566 | Southend Airport / Hans Ulricksen Field | small_airport | 56.337200 | -103.292999 | 1120.0 | Saskatchewan | Southend | POINT (5512531.791 2297534.809) |
| 910 | Nueltin Lake Airport | small_airport | 59.708099 | -100.126999 | 971.0 | Manitoba | Nueltin Lake | POINT (5749138.148 2630980.068) |
| 1385 | Kasba Lake Airport | small_airport | 60.291901 | -102.501999 | 1131.0 | Northwest Territories | Kasba Lake | POINT (5631102.710 2712557.907) |
| 507 | Camsell Portage Airport | small_airport | 59.610001 | -109.266998 | 750.0 | Saskatchewan | Camsell Portage | POINT (5256550.930 2720892.491) |
| 866 | Jean Marie River Airport | small_airport | 61.522449 | -120.625334 | 470.0 | Northwest Territories | Jean Marie River | POINT (4764765.333 3128139.050) |
| 524 | Ford Bay Airport | small_airport | 66.037498 | -124.714996 | 673.0 | Northwest Territories | Ford Bay | POINT (4818865.950 3650884.970) |
| 762 | Streatham Reef Hueston Airfield | small_airport | 53.822569 | -126.172957 | NaN | British Columbia | Streatham | POINT (4082935.011 2546192.998) |
| 912 | Ospika Airport | small_airport | 56.275002 | -124.052002 | 2300.0 | British Columbia | Ospika | POINT (4331846.564 2711847.273) |
| 654 | Fort Grahame Airport | small_airport | 56.521388 | -124.470291 | 2230.0 | British Columbia | Fort Graham | POINT (4322874.291 2747586.305) |
| 711 | Tsay Keh Airport | small_airport | 56.906101 | -124.964996 | 2280.0 | British Columbia | Tsay Keh | POINT (4317803.890 2798610.897) |
| 1234 | Carcross Airport | small_airport | 60.173762 | -134.697683 | 2161.0 | Yukon | Carcross | POINT (4060280.232 3401031.367) |
| 801 | Carmacks Airport | small_airport | 62.110630 | -136.179993 | 1770.0 | Yukon | Carmacks | POINT (4132820.142 3612334.498) |
| 1260 | Wheatly (Robinson Motorcycles) Airfield | small_airport | 42.143056 | -82.362500 | 617.0 | Ontario | Coatsworth | POINT (7008025.086 715796.100) |
| 1518 | Rednersville / Aery | small_airport | 44.105299 | -77.459149 | 390.0 | Ontario | Prince Edward County | POINT (7368703.649 1012129.458) |
| 1507 | Port Elgin (Pryde Field) Airport | small_airport | 44.458900 | -81.379700 | 661.0 | Ontario | Port Elgin | POINT (7047529.961 989340.169) |
| 123 | Killarney Airport | small_airport | 45.977501 | -81.494698 | 608.0 | Ontario | Killarney | POINT (7010470.567 1157081.090) |
| 783 | Bar River Airport | small_airport | 46.420300 | -84.092201 | 591.0 | Ontario | Bar River | POINT (6802610.805 1177528.280) |
| 652 | Florenceville Airport | small_airport | 46.426102 | -67.628098 | 508.0 | New Brunswick | Florenceville-Bristol | POINT (8037957.683 1495804.699) |
| 1615 | Upper Kent Airport | small_airport | 46.587133 | -67.719555 | 246.0 | New Brunswick | Upper Kent | POINT (8024657.232 1509965.758) |
| 837 | Thunderbay / Eldorado Field | small_airport | 48.572222 | -88.816667 | 700.0 | Ontario | Eldorado | POINT (6425370.594 1386781.418) |
| 506 | Bloodvein River Airport | small_airport | 51.784568 | -96.692305 | 721.0 | Manitoba | Bloodvein River | POINT (5870595.741 1749660.251) |
# simple plot
base=riversAll_bufDF.plot(color='blue',linewidth=15)
allRiversWithinBuffs.plot(ax=base, color='pink', markersize=5)
<Axes: >
# folium (mapa interactivo)
base=riversAll_bufDF.explore(color='thistle')
allRiversWithinBuffs.explore(m=base, color='pink')